<script setup>
import { Delete, Edit,Plus } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import ChannelEdit from './components/ChannelEdit.vue'
import { searchList } from '@/api/cate'
const channelList = ref([])
const loading = ref(false)
const cateParm = ref({
  pageNo: 1,
  pageSize: 500
})
const getChannelList = async () => {
  loading.value = true
  const {
    data : { data: { data: { list }} }
  } = await searchList(cateParm.value)
  channelList.value = list 
  console.log(channelList.value);
  
  
  loading.value = false
}
getChannelList()

const dialog = ref(null)
const onEditChannel = async (row) => {
  dialog.value.open(row,null)
}
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}
const onAddChannel = () => {
  dialog.value.open({},null)
}
const plusChannel = (row) => {
  dialog.value.open(null,row.id)
}
const onSuccess = () => {
  getChannelList()
}

</script>

<template>
  
  <page-container title="商品分类管理" ref="edit"
    ><template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>

    <el-table
  
    style="width: 100%"
      row-key="id"
      border
      lazy
      load
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      :data="channelList"
    >
      
      <el-table-column prop="name" label="分类名称" />
      <el-table-column prop="image" label="分类图片">
        <template #default="{ row }">
          <img :src="row.image" alt="" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="creatorName" label="创建者" />
      <el-table-column align="right" width="150">
        
        <template #default="{ row }">
          <el-button
            @click="plusChannel(row)"
            type="primary"
            :icon="Plus"
            circle
            plain
          >
          </el-button>
          <el-button
            @click="onEditChannel(row)"
            type="primary"
            :icon="Edit"
            circle
            plain
          >
          </el-button>

          <el-button
            @click="onDelChannel(row)"
            type="danger"
            :icon="Delete"
            circle
            plain
          ></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
  </page-container>

  <ChannelEdit @success="onSuccess" ref="dialog"></ChannelEdit>
</template>
